/**
 * FssLogger.java
 */
package fss.core.logger;

import java.util.ResourceBundle;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

import fss.core.FssThrowable;

/**
 * This class is used to set up the Java Logger for the application to use.
 * <p>
 * All logger objects in this application will have a file handler and
 * console handler.
 * 
 * @author thinh
 *
 */
public class FssLogger {
    /**
     * The properties file.
     */
    private static String LOGGER_PROPERTIES = "fss";
    /**
     * The file handler.
     */
    private static FileHandler fileHandler = null;
    /**
     * The console handler.
     */
    private static ConsoleHandler consoleHandler = null;
    /**
     * The singleton.
     */
    private static FssLogger instance = null;
    /**
     * Hidden constructor.
     */
    private FssLogger() {
        setup();
    }
    /**
     * Get the instance.
     * @return the instance.
     */
    public static FssLogger getInstance() {
        if(instance == null) {
            instance = new FssLogger();
        }
        return instance;
    }
    /**
     * Create a new logger.
     * @param name logger identifier.
     * @return a logger.
     */
    public Logger getLogger(String name) {
        return Logger.getLogger(name);
    }
    /**
     * The setup method.
     */
    private static void setup() {
        Logger logger = null;
        ResourceBundle properties = ResourceBundle.getBundle(LOGGER_PROPERTIES);
        String logDir = properties.getString("fsslogger.home");
        String logFilename = properties.getString("fsslogger.filename");
        String separator = System.getProperties().getProperty("file.separator");
        String logFile = logDir + separator + logFilename;
        
        try {            
            consoleHandler = new ConsoleHandler();
            // TODO: console formatter duplicates log entries when adding a specific formatter
//            consoleHandler.setFormatter(new FssFormatter());
            fileHandler = new FileHandler(logFile, true);
            fileHandler.setFormatter(new FssFormatter());
            logger = Logger.getLogger("");
            logger.addHandler(consoleHandler);
            logger.addHandler(fileHandler);
        } catch (Exception e) {
            throw new FssThrowable("Error initializing the FssLogger", e);
        }
    }
}